<template>
  <div class="search">
    <Card>
      <Form ref="searchForm" :model="searchForm" inline :label-width="70" class="search-form">
        <Form-item label="店铺名称" prop="storeName">
          <Input
            type="text"
            v-model="searchForm.storeName"
            placeholder="请输入店铺名称"
            clearable
            style="width: 200px"
          />
        </Form-item>
        <Form-item label="店铺状态">
          <Select v-model="searchForm.storeDisable" clearable style="width: 200px">
            <Option value="OPEN">开启中</Option>
            <Option value="CLOSED">已关闭</Option>
            <Option value="APPLY">申请中</Option>
            <Option value="APPLYING">审核中</Option>
            <Option value="REFUSED">审核拒绝</Option>
          </Select>
        </Form-item>
        <Form-item label="创建时间" prop="createTime">
          <DatePicker
            v-model="selectDate"
            type="datetimerange"
            format="yyyy-MM-dd HH:mm:ss"
            clearable
            @on-change="selectDateRange"
            placeholder="选择起始时间"
            style="width: 200px"
          ></DatePicker>
        </Form-item>
        <Button @click="handleSearch" type="primary" icon="ios-search" class="search-btn"
          >搜索</Button
        >
      </Form>
      <Row class="operation padding-row">
        <Button @click="add" type="primary">商户进件</Button>
      </Row>
      <Table :loading="loading" border :columns="columns" :data="data" ref="table">
        <template slot-scope="{ row }" slot="signState">
          <Tag v-if="row.signState == 'SIGNED'" color="success">已签约</Tag>
          <Tag v-else-if="row.signState == 'NOT_SIGNABLE'" color="error">不可签约</Tag>
          <Tag v-else>未签约</Tag>
        </template>
        <template slot-scope="{ row }" slot="memo">
          <Poptip trigger="hover" title="驳回原因" transfer>
            <div slot="content">
              <Table
                :width="500"
                border
                :data="JSON.parse(row.auditDetail || '[]')"
                :columns="[
                  { title: '参数名称', key: 'param_name', width: 150 },
                  { title: '驳回原因', key: 'reject_reason' },
                ]"
              ></Table>
            </div>
            <a>查看</a>
          </Poptip>
        </template>
      </Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
  </div>
</template>

<script>
import { getPageData, disableShop, enableBrand, shopAudit } from "@/api/shops-sft";
export default {
  name: "shop",
  data() {
    return {
      loading: true, // 表单加载状态
      searchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        sort: "createTime", // 默认排序字段
        order: "desc", // 默认排序方式
        startDate: "", // 起始时间
        endDate: "", // 终止时间
      },
      selectDate: null, // 创建时间
      columns: [
        // 表头
        {
          title: "店铺名称",
          key: "storeName",
          minWidth: 120,
          align: "left",
        },
        {
          title: "申请单号",
          key: "applymentId",
          width: 150,
          tooltip: true,
          align: "center",
        },
        {
          title: "二级商户号",
          key: "subMchid",
          width: 150,
          tooltip: true,
          align: "center",
        },
        {
          title: "状态",
          key: "orderId",
          width: 150,
          tooltip: true,
          align: "center",
          render: (h, params) => {
            const row = params.row;
            switch (row.status) {
              case "FINISH":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "success",
                    },
                  },
                  "完成",
                );
              case "REJECTED":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "error",
                    },
                  },
                  "已驳回",
                );
              case "AUDITING":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "primary",
                    },
                  },
                  "审核中",
                );
              case "CHECKING":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "primary",
                    },
                  },
                  "资料验证",
                );
              case "ACCOUNT_NEED_VERIFY":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "warning",
                    },
                  },
                  "待商户验证",
                );
              case "NEED_SIGN":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "warning",
                    },
                  },
                  "待签约",
                );
              case "FROZEN":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "blue",
                    },
                  },
                  "已冻结",
                );
              case "CANCELED":
                return h(
                  "Tag",
                  {
                    props: {
                      color: "red",
                    },
                  },
                  "已作废",
                );
              default:
                return h("Tag", {}, "未进件");
            }
          },
        },
        {
          title: "签约状态",
          key: "signState",
          width: 120,
          tooltip: true,
          align: "center",
          slot: "signState",
        },
        {
          title: "签约链接",
          key: "signUrl",
          minWidth: 150,
          tooltip: true,
        },
        {
          title: "法人验证链接",
          key: "legalValidationUrl",
          minWidth: 150,
          tooltip: true,
        },
        {
          title: "驳回原因",
          key: "memo",
          slot: "memo",
          width: 100,
          align: "center",
        },
        {
          title: "创建时间",
          key: "createTime",
          align: "left",
          width: 170,
        },
        {
          title: "操作",
          key: "action",
          width: 200,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.detail(params.row);
                    },
                  },
                },
                "查看",
              ),
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small",
                    ghost: true,
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    },
                  },
                },
                "修改",
              ),
            ]);
          },
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
      selectedShop: false, //用于是否选择店铺
    };
  },

  methods: {
    // 回调给父级
    callback(val) {
      this.$emit("callback", val);
    },
    // 初始化数据
    init() {
      this.getDataList();
    },
    // 分页 改变页码
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    // 搜索
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    // 起止时间分别赋值
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    // 获取列表数据
    getDataList() {
      this.loading = true;
      // 带多条件搜索参数获取表单数据 请自行修改接口
      getPageData(this.searchForm).then(res => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.records;
          this.total = res.result.total;
        }
      });
      this.total = this.data.length;
      this.loading = false;
    },
    // 添加店铺
    add() {
      this.$router.push({ path: "/shop-sft-create", query: { formType: "add" } });
    },
    // 修改店铺
    edit(v) {
      this.$router.push({ path: "/shop-sft-create", query: { id: v.id, formType: "edit" } });
    },
    //查看店铺详细
    detail(row) {
      this.$router.push({ path: "/shop-sft-create", query: { id: row.id, formType: "view" } });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
